/**
 * User: Paulo Fournier
 * Date: 17-08-2013
 * Used as a general filter list plugin
 */

Ext.define('Mobile-app.controller.FilterList', {
    extend: 'Ext.app.Controller',

    config: {
        //add view references for this controller
        refs: {
            mobilePanel: 'mobilepanel',
            poiPanel: 'poipanel',
            driverPanel: 'driverpanel',
            trailerPanel: 'trailerpanel'
        },

        //Definition of all listen events of this controller
        //can be triggered by the [fireEvent] method
        control: {

            mobilePanel: {
                filterList: 'onFilterList',
                clearFilter: 'onClearFilter'
            },
            poiPanel: {
                filterList: 'onFilterList',
                clearFilter: 'onClearFilter'
            },
            driverPanel: {
                filterList: 'onFilterList',
                clearFilter: 'onClearFilter'
            },
            trailerPanel: {
                filterList: 'onFilterList',
                clearFilter: 'onClearFilter'
            }

        }

    },

    /**
     * Will be triggered with the filterList event
     * @param view The list view
     * @param list The list panel
     * @param field The filter field
     */
    onFilterList: function(view, list, field){

        var store = list.getStore(),
            fields = store.getModel().getFields(),
            value = field.getValue();

        //first clear any current filters on these store
        store.clearFilter();

        if(value){
            //the user could have entered spaces, so we must split them so we can loop through them all
            var searches = value.split(' '),
                regexps = [],
                i;

            //loop them all
            for (i = 0; i < searches.length; i++) {
                //if it is nothing, continue
                if (!searches[i]) continue;

                //if found, create a new regular expression which is case insenstive
                regexps.push(new RegExp(searches[i], 'i'));
            }

            //now filter the store by passing a method
            //the passed method will be called for each record in the store
            store.filter(function(record) {
                var matched = [];

                //loop through each of the regular expressions
                for (i = 0; i < regexps.length; i++) {
                    var search = regexps[i],
                        didMatch = false; /*record.get('reference').match(search) ||
                     record.get('tagGroup').match(search) ||
                     record.get('id').toString().match(search);
                     */

                    var items = fields.items;

                    for(i in items){
                        //check if field is set as filterable
                        if(items[i].initialConfig.filterable){
                            //get field value
                            var value = record.get(items[i].getName());
                            //check if any match was found
                            if(value && value.toString().match(search)){
                                didMatch = true;
                                break;
                            }
                        }
                    }

                    //if it matched the first or last name, push it into the matches array
                    matched.push(didMatch);
                }

                //if nothing was found, return false (dont so in the store)
                if (regexps.length > 1 && matched.indexOf(false) != -1) {
                    return false;
                } else {
                    //else true true (show in the store)
                    return matched[0];
                }
            });
        }
    },

    /***
     * Will clear the filter
     * @param list
     */
    onClearFilter: function(list){

        //call the clearFilter method on the store instance
        list.getStore().clearFilter();
    }

});